package com.ruoyi.web.controller.easywenku;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.easywenku.entity.QuestionType;
import com.ruoyi.easywenku.service.IQuestionTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目分类Controller
 *
 * @author yx
 * @date 2020-11-17
 */
@Controller
@RequestMapping("/easywenku/type")
public class QuestionTypeController extends BaseController
{
    private String prefix = "easywenku/type";

    @Autowired
    private IQuestionTypeService questionTypeService;

    @RequiresPermissions("easywenku:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询题目分类列表
     */
    @RequiresPermissions("easywenku:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QuestionType questionType)
    {
        startPage();
        List<QuestionType> list = questionTypeService.selectQuestionTypeList(questionType);
        return getDataTable(list);
    }

    /**
     * 导出题目分类列表
     */
    @RequiresPermissions("easywenku:type:export")
    @Log(title = "题目分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QuestionType questionType)
    {
        List<QuestionType> list = questionTypeService.selectQuestionTypeList(questionType);
        ExcelUtil<QuestionType> util = new ExcelUtil<QuestionType>(QuestionType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增题目分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存题目分类
     */
    @RequiresPermissions("easywenku:type:add")
    @Log(title = "题目分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QuestionType questionType)
    {
        return toAjax(questionTypeService.insertQuestionType(questionType));
    }

    /**
     * 修改题目分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        QuestionType questionType = questionTypeService.selectQuestionTypeById(id);
        mmap.put("questionType", questionType);
        return prefix + "/edit";
    }

    /**
     * 修改保存题目分类
     */
    @RequiresPermissions("easywenku:type:edit")
    @Log(title = "题目分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QuestionType questionType)
    {
        return toAjax(questionTypeService.updateQuestionType(questionType));
    }

    /**
     * 删除题目分类
     */
    @RequiresPermissions("easywenku:type:remove")
    @Log(title = "题目分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(questionTypeService.deleteQuestionTypeByIds(ids));
    }
}
